﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using ConjureBivouac2012.Mathematics;

namespace ConjureBivouac2012.Object
{
    public abstract class AbstractEnemy : Entity
    {
        // Ennemy's health
        protected int health;
        protected bool canDie = false;
        protected string contentName = "Ennemy";
        protected Random random = new Random();

        private int _size;

        protected ContentManager _contentManager;

        protected AbstractEnemy(int size, int health) : base()
        {
            Initialize();
            this.LoadContent(GameInfo.GetInstance().GetContent());
            animationDelay = 1000;
            this.health = health;
            myName = ENNEMY;

            if (size > 0)
            {
                _size = size;
                this.sprite.Scale = (float)_size / sprite.Source.Width;
            }
        }

        public override void Update(Microsoft.Xna.Framework.GameTime time)
        {
            if (health > 0)
            {
                base.Update(time);
                //Fire(time);
                //StageCollision();
                if (animationCount > 0)
                {
                    if (animationCount == 1)
                    {
                        lastTime = time.TotalGameTime;
                        animationCount = 2;
                    }
                    if (animationCount == 2)
                    {
                        if (time.TotalGameTime.TotalMilliseconds - lastTime.TotalMilliseconds > animationDelay)
                        {
                            sprite.LoadContent(GameInfo.GetInstance().GetContent(), contentName);
                            animationCount = 0;
                            lastTime = time.TotalGameTime;
                        }
                    }
                }
            }
            else
            {
                animationDelay = 150;
                if (animationCount != 16)
                {
                    if (time.TotalGameTime.TotalMilliseconds - lastTime.TotalMilliseconds > animationDelay)
                    {
                        animationCount++;
                        sprite.LoadContent(GameInfo.GetInstance().GetContent(), "EnnemyDied/EnnemyDied" + animationCount);
                        lastTime = time.TotalGameTime;
                    }
                }
                else
                {
                    canDie = true;
                }
            }

            if (_size > 0)
            {
                this.sprite.Scale = (float)_size / sprite.Source.Width;
            }

            base.Update(time);
        }

        public override Entity DroppingEntity()
        {
            Entity power = null;

            int i = random.Next(1, 100);

            if (i <= 70)
            {
                power = new ShieldIncrease(20);
            }
            else if (i <= 95)
            {
                power = new HealthIncrease(2);
            }
            else
            {
                power = new SuperBomb();
            }

            if(power != null)
            {
                power.sprite.Position = this.sprite.Position;
            }
            

            return power;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            base.LoadContent(contentManager);
            _contentManager = contentManager;
            sprite.Depth = 0.1f;
        }

        public override void destroyMe()
        {
            if (health > 0)
            {
                health--;
                animationCount = 1;
                int hit = random.Next(2) + 1;
                sprite.LoadContent(GameInfo.GetInstance().GetContent(), "EnnemyHit/EnnemyHit" + hit);
                if (_size > 0)
                {
                    this.sprite.Scale = (float)_size / sprite.Source.Width;
                }
            }
        }

        public override bool hasToBeDestroyed()
        {
            return (health <= 0 && canDie);
        }
    }
}
